package com.z.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.z.base.PageTableRequest;
import com.z.base.ResponseCode;
import com.z.base.Results;
import com.z.sys.model.Permission;
import com.z.sys.service.IPermissionService;
import com.z.sys.vo.DTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxy
 * @since 2020-10-14
 */
@Controller
@RequestMapping("/sys/permission")
@Api(tags = "权限相关")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/toList")
    @ApiOperation("权限列表页")
    public String toList(){
        return "sys/permission/permissionList";
    }

    @ResponseBody
    @PostMapping("/treeInfo")
    @ApiOperation("权限树")
    public Results treeInfo(){
        List<DTree> datas = permissionService.selectTree();
        return Results.success(0,"查询成功", datas);
    }
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation("权限列表")
    public Results list(String nodeId, PageTableRequest page, HttpServletRequest request){

        QueryWrapper<Permission> query = new QueryWrapper<Permission>().nested(i->i.eq("parent_id", nodeId).or().eq("id", nodeId));

        String cloumn = request.getParameter("cloumn");
        String keyword = request.getParameter("keyword");

        if(!StringUtils.isEmpty(cloumn) && !StringUtils.isEmpty(keyword))
            query.and(i->i.like(cloumn, keyword));
        List<Permission> datas = permissionService.getBaseMapper().selectList(query.orderByAsc("parent_Id", "sort"));
        return Results.success( permissionService.getBaseMapper().selectCount(query),"查询成功", datas);
    }

    @ApiOperation("权限添加页面")
    @GetMapping("toAdd")
    public String toAdd(Model model, Permission permission) {
        model.addAttribute("sysPermission", permission);
        return "sys/permission/permissionAdd";
    }
    @GetMapping("toEdit")
    @ApiOperation("权限编辑页面")
    public String toEdit(Model model, String id) {
        model.addAttribute("sysPermission", permissionService.getById(id));
        return "sys/permission/permissionEdit";
    }
    @PostMapping("add")
    @ApiOperation("权限添加接口")
    @ResponseBody
    public Results add(Permission permission){
        try{
            permission.setCreateTime(new Date());
            permissionService.save(permission);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("update")
    @ApiOperation("权限更新接口")
    @ResponseBody
    public Results update(Permission permission){
        try{

            permission.setUpdateTime(new Date());
            permissionService.updateById(permission);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("del")
    @ApiOperation("权限删除-单个")
    @ResponseBody
    public Results del(String id){
        try{
            permissionService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("delAll")
    @ApiOperation("权限删除接口-多个")
    @ResponseBody
    public Results delAll(String ids){
        try{
            permissionService.removeByIds(Arrays.asList(ids.split(",")));
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }

    @PostMapping("menu")
    @ApiOperation("获取左侧菜单")
    @ResponseBody
    public Results menu(String userId){

        try{
           List<DTree> permissions = permissionService.getMenu(userId);
           return Results.success(permissions);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
    }

}
